/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.app.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import com.thinkgem.jeesite.modules.app.entity.OrderGoods2;
import com.thinkgem.jeesite.modules.app.service.OrderGoods2Service;
import com.thinkgem.jeesite.modules.menu.entity.Order3;
import com.thinkgem.jeesite.modules.menu.entity.OrderGoods3;
import com.thinkgem.jeesite.modules.shop.entity.Order;
import com.thinkgem.jeesite.modules.shop.service.OrderGoodsService;
import com.thinkgem.jeesite.modules.shop.service.OrderService;
import com.thinkgem.jeesite.test.entity.TestDataMain;
import com.thinkgem.jeesite.test.service.TestDataMainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.app.entity.Order2;
import com.thinkgem.jeesite.modules.app.service.Order2Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单Controller
 * @author dby
 * @version 2020-07-14
 */
@Controller
@RequestMapping(value = "${adminPath}/app/order2")
public class Order2Controller extends BaseController {

	@Autowired
	private Order2Service order2Service;


	@Autowired
	private OrderService orderService;

	@Autowired
	private TestDataMainService testDataMainService;


	@ModelAttribute
	public Order2 get(@RequestParam(required=false) String id) {
		Order2 entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = order2Service.get(id);
		}
		if (entity == null){
			entity = new Order2();
		}
		return entity;
	}
	
	@RequiresPermissions("app:order2:view")
	@RequestMapping(value = {"list", ""})
	public String list(Order2 order2, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Order2> page = order2Service.findPage(new Page<Order2>(request, response), order2); 
		model.addAttribute("page", page);
		return "modules/app/order2List";
	}
//
//	@RequiresPermissions("app:order2:view")
//	@RequestMapping(value = "form")
//	public String form(Order2 order2, Model model) {
//		model.addAttribute("order2", order2);
//		return "modules/app/order2Form";
//	}
//
//	@RequiresPermissions("app:order2:edit")
//	@RequestMapping(value = "save")
//	public String save(Order2 order2, Model model, RedirectAttributes redirectAttributes) {
//		if (!beanValidator(model, order2)){
//			return form(order2, model);
//		}
//		order2Service.save(order2);
//		addMessage(redirectAttributes, "保存订单成功");
//		return "redirect:"+Global.getAdminPath()+"/app/order2/?repage";
//	}





	@RequiresPermissions("app:order2:view")
	@RequestMapping(value = "form")
	public String form(Order2 order2, Model model) {

		List<OrderGoods2> orderGoodsList = null;

		//新增时  order id 为空
		if(StringUtils.isBlank(order2.getId())){

		}else{
			OrderGoods2 orderGoods2 =  new OrderGoods2();
			orderGoods2.setOrderId(order2.getId());
			orderGoodsList = orderGoods2Service.findList(orderGoods2);
		}

		if (null == orderGoodsList || orderGoodsList.isEmpty()) {
			orderGoodsList = new ArrayList<>();
			orderGoodsList.add(new OrderGoods2());
		}
		model.addAttribute("order2", order2);
		model.addAttribute("orderGoodsList", orderGoodsList);

		return "modules/app/order2Form";
	}

	@RequiresPermissions("app:order2:edit")
	@RequestMapping(value = "save")
	public String save(Order2 order2, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, order2)){
			return form(order2, model);
		}
		try {
			order2Service.saveOrder(order2);
			addMessage(redirectAttributes, "保存订单成功");
		} catch (Exception e) {
			e.printStackTrace();
			addMessage(redirectAttributes, "保存订失败");
		}
		return "redirect:"+Global.getAdminPath()+"/app/order2/?repage";
	}


	@RequiresPermissions("app:order2:edit")
	@RequestMapping(value = "delete")
	public String delete(Order2 order2, RedirectAttributes redirectAttributes) {
		order2Service.delete(order2);
		addMessage(redirectAttributes, "删除订单成功");
		return "redirect:"+Global.getAdminPath()+"/app/order2/?repage";
	}


	@RequiresPermissions("app:order2:view")
	@RequestMapping(value = {"myOrderList"})
	public String  List1(Order2 order2, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Order2> page = order2Service.findPage(new Page<Order2>(request, response), order2);
		model.addAttribute("page", page);
		return "modules/app/myOrderList";
	}


	@Autowired
	private OrderGoods2Service orderGoods2Service;
	/*
	 * @Author Zhang Xingliang
	 * @Description 下单 在Controller 层 完成订单保存和订单商品保存
	 * @Date 22:38 2020/7/7
	 **/
	@RequestMapping(value = "saveOrder")
	@ResponseBody
	public ResponseResult save(Order2 order2) {
		order2Service.save(order2);
		orderGoods2Service.insertList(order2.getOrderGoodsList());
		return ResponseResult.success();
	}

	/*
	 * @Author Zhang Xingliang
	 * @Description 下单 在orderService 去完成 订单保存 及订单商品保存 一般 使用本方法 形式
	 *
	 *  本方法 前端html页面 需要使用ajax 方式提交表单 传入 order对象
	 *
	 * @Date 22:38 2020/7/7
	 **/
	@RequestMapping(value = "saveOrder1")
	@ResponseBody
	public ResponseResult save(@RequestBody Order order) {
		orderService.save(order);
		return ResponseResult.success();
	}




//
//	@RequiresPermissions("test:testDataMain:view")
//	@RequestMapping(value = {"list1", ""})
//	public String list1(TestDataMain testDataMain, HttpServletRequest request, HttpServletResponse response, Model model) {
//		Page<TestDataMain> page = testDataMainService.findPage(new Page<TestDataMain>(request, response), testDataMain);
//		model.addAttribute("page", page);
//		return "modules/app/order2DataMainList";
//	}
//
//	@RequiresPermissions("test:testDataMain:view")
//	@RequestMapping(value = "form2")
//	public String form2(TestDataMain testDataMain, Model model) {
//		model.addAttribute("testDataMain", testDataMain);
//		return "modules/app/order2DataMainForm";
//	}


}